package com.zhonglian.education.customerview.seeimg;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.zhonglian.education.R;

import java.util.List;

import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by yx on 2017/6/28.
 */

public class ViewPageAdapter extends PagerAdapter {
    private Context context;
    private List<String> images;
    /**
     * SparseArray比HashMap更省内存，在某些条件下性能更好，主要是因为它避免了对key的自动装箱（int转为Integer类型），
     * 它内部则是通过两个数组来进行数据存储的，一个存储key，另外一个存储value，为了优化性能，
     * 它内部对数据还采取了压缩的方式来表示稀疏数组的数据，从而节约内存空间
     */
    private SparseArray<View> cacheView;
    private ViewGroup containerTemp;

    public ViewPageAdapter(Context context, List<String> images) {
        this.context = context;
        this.images = images;
        cacheView = new SparseArray<>(images.size());
    }

    @Override
    public Object instantiateItem(final ViewGroup container, int position) {
        if (containerTemp == null) containerTemp = container;
        View view = cacheView.get(position);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.vp_item_image, container, false);
            view.setTag(position);
            final ImageView image = (ImageView) view.findViewById(R.id.image);
            final PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(image);//设置IV可放大缩小
            //Picasso加载图片，565提高图片加载效率
            if (!TextUtils.isEmpty(images.get(position))) {
                Picasso.with(context).load(images.get(position)).config(Bitmap.Config.RGB_565).into(image, new Callback() {
                    @Override
                    public void onSuccess() {
                        //在Picasso的回调中调用PhotoViewAttacher的Update刷新，不刷新的话会导致图片错位。
                        photoViewAttacher.update();
                        //LoadingIconDialogUtil.hidn();//关闭加载中
                    }

                    @Override
                    public void onError() {
                        //LoadingIconDialogUtil.hidn();//关闭加载中
                    }
                });
            }
            photoViewAttacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {//用户点击图片的回调
                    Activity activity = (Activity) context;
                    activity.finish();
                }

                @Override
                public void onOutsidePhotoTap() {

                }
            });
            cacheView.put(position, view);
        }
        container.addView(view);
        return view;
    }

    @Override
    public int getCount() {
        return images != null ? images.size() : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);
    }


}
